#include "landsprite.h"
#include"globalgamemsger.h"
#include<QPainter>
QImage landSprite::landImg(":/ImgSrc/Images/land.png");
const QImage&landSprite::landImage=landImg;
landSprite::landSprite():offset(0)
{
    speed=standardspeed;
    setZValue(2);
    connect(&globalGameMsger::m,&globalGameMsger::gameBegin,this,&landSprite::onGameBegin);
    connect(&globalGameMsger::m,&globalGameMsger::gameOver,this,&landSprite::onGameOver);
    connect(&globalGameMsger::m,&globalGameMsger::gameRestart,this,&landSprite::onGameRestart);
}

void landSprite::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QRectF r=landImg.rect();
    painter->drawImage(QRectF(offset,0,r.width()-offset,r.height()),landImg,QRectF(0,0,r.width()-offset,r.height()));
    painter->drawImage(QRectF(0,0,offset,r.height()),landImg,QRectF(r.width()-offset,0,offset,r.height()));
}

QRectF landSprite::boundingRect() const
{
    return landImg.rect();
}

void landSprite::advance(int phase)
{
    if(phase==0)return;
    offset-=speed;
    offset%=landImg.rect().width();
    if(offset<0)offset+=landImg.rect().width();
}

void landSprite::onGameBegin()
{
}

void landSprite::onGameOver()
{
    speed=0;
}

void landSprite::onGameRestart()
{
    speed=standardspeed;
}
